package com.yueqian.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yueqian.framework.enmus.ExceptionEnum;
import com.yueqian.framework.entity.BaseEntity;
import com.yueqian.framework.exception.GlobalException;
import com.yueqian.system.converter.DeptConvert;
import com.yueqian.system.entity.Dept;
import com.yueqian.system.mapper.DeptMapper;
import com.yueqian.system.service.DeptService;
import com.yueqian.system.vo.req.DeptReqVO;
import com.yueqian.system.vo.res.DeptResVO;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
	@Override
	public Page<Dept> selectPageData(DeptReqVO reqVO) {
		IPage<Dept> page = reqVO.getPage(reqVO.getCurrentPage(), reqVO.getPageSize());
		return (Page<Dept>) this.baseMapper.selectPage(page, Wrappers.lambdaQuery(Dept.class)
																	 .eq(StrUtil.isNotBlank(reqVO.getName()), Dept::getName, reqVO.getName()));
	}

	@Override
	public List<DeptResVO> selectListData(DeptReqVO reqVO) {
		reqVO.setId(0L);
		List<DeptResVO> deptList = this.getDeptChildren(reqVO);
		if (CollectionUtil.isEmpty(deptList)) {
			return new ArrayList<>();
		}
		List<DeptResVO> deptResVOList = deptList.stream().peek(item -> {
			DeptReqVO req = DeptConvert.INSTANCE.resToReq(item);
			List<DeptResVO> deptChildren = this.getDeptChildren(req);
			item.setChildren(deptChildren);
		}).collect(Collectors.toList());
		return this.filterDept(deptResVOList, reqVO);
	}


	/**
	 * 根据条件过滤部门
	 *
	 * @param deptResVOList
	 * @param reqVO
	 */
	public List<DeptResVO> filterDept(List<DeptResVO> deptResVOList, DeptReqVO reqVO) {
		if (CollectionUtil.isEmpty(deptResVOList)) {
			return new ArrayList<>();
		}
		if (ObjectUtil.isEmpty(reqVO.getName())) {
			return deptResVOList;
		}
		ArrayList<DeptResVO> deptResVOS = new ArrayList<>();
		for (DeptResVO deptResVO : deptResVOList) {
			//只有一层节点
			if (StrUtil.contains(deptResVO.getName(), reqVO.getName()) && CollectionUtil.isEmpty(deptResVO.getChildren())) {
				deptResVOS.add(deptResVO);
			}
			//包含子节点
			if (CollectionUtil.isNotEmpty(deptResVO.getChildren())) {
				//父节点名称满足同时，又有子节点数据
				if (StrUtil.contains(deptResVO.getName(), reqVO.getName())) {
					deptResVOS.add(deptResVO);
					deptResVO.setChildren(filterDept(deptResVO.getChildren(), reqVO));
				} else {
					//父节点不满足，子节点有数据
					List<DeptResVO> menuTreeResVOS1 = filterDept(deptResVO.getChildren(), reqVO);
					CollUtil.addAll(deptResVOS, menuTreeResVOS1);
				}
			}

		}
		return deptResVOS;
	}

	@Transactional
	@Override
	public Boolean add(List<DeptReqVO> reqVOS) {
		if (CollectionUtil.isEmpty(reqVOS)) {
			return false;
		}
		List<Dept> list = DeptConvert.INSTANCE.toList(reqVOS);
		return this.saveBatch(list);
	}

	@Override
	public Long add(DeptReqVO reqVO) {
		Dept entity = DeptConvert.INSTANCE.toEntity(reqVO);
		//编辑
		if (ObjectUtil.isNotEmpty(reqVO.getId())) {
			this.saveOrUpdate(entity);
		}
		//新增
		this.saveOrUpdate(entity);
		return entity.getId();
	}

	@Transactional
	@Override
	public Boolean delete(List<Long> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return false;
		}
		return this.removeByIds(ids);
	}

	@Override
	public Long delete(Long id) {
		this.removeById(id);
		return id;
	}

	@Override
	public List<Dept> getDetail(List<Long> ids) {
		if (CollectionUtil.isNotEmpty(ids)) {
			ArrayList<Dept> deptList = new ArrayList<>();
			deptList.add(new Dept());
			return deptList;
		}
		return this.listByIds(ids);
	}

	@Override
	public DeptResVO getDetail(Long id) {
		Dept dept;
		this.isMenuExist();
		if (ObjectUtil.isNull(this.getById(id))) {
			dept = new Dept();
		} else {
			dept = this.getById(id);
		}
		DeptResVO deptResVO = DeptConvert.INSTANCE.toRes(dept);
		//设置子节点
		DeptReqVO reqVO = new DeptReqVO();
		reqVO.setId(0L);
		deptResVO.setChildren(this.getDeptChildren(reqVO));
		return deptResVO;
	}

	public List<DeptResVO> getDeptChildren(DeptReqVO reqVO) {
		List<Dept> deptList;
		//加载根节点下面的节点
		deptList = this.list(Wrappers.lambdaQuery(Dept.class).eq(Dept::getParentId, reqVO.getId()).orderByAsc(Dept::getSort).orderByDesc(BaseEntity::getCreatorTime));
		List<DeptResVO> resList = DeptConvert.INSTANCE.toResList(deptList);
		for (DeptResVO deptResVO : resList) {
			DeptReqVO req = DeptConvert.INSTANCE.resToReq(deptResVO);
			deptResVO.setChildren(this.getDeptChildren(req));
		}
		return resList;
	}


	/**
	 * 判断部门数据是否存在
	 */
	public void isMenuExist() {
		//判断是否存在菜单根节点
		//查找根节点
		List<Dept> isExistDeptList = this.list(Wrappers.lambdaQuery(Dept.class).eq(Dept::getParentId, 0));
		if (CollectionUtil.isEmpty(isExistDeptList)) {
			ExceptionEnum priceCannotBeNull = ExceptionEnum.MENU_IS_NULL;
			throw new GlobalException(priceCannotBeNull);
		}
	}
}
